package org.qydjk.console.controller.product;


import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.product.ProductRelated;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/relatedProduct/")
public class RelatedProductController extends BaseController{

    /**
     * 相关产品页面跳转
     * @return
     */
    @RequestMapping("/relatedProduct.do")
    public String relatedProduct(){
        return "product/relatedProduct";
    }

    /**
     * 相关产品数据
     * @param page
     * @param rows
     * @param searchType
     * @param shopNameSearch
     * @param productNameSearch
     * @param productId
     * @return
     */
    @RequestMapping("/relatedProductList.do")
    @ResponseBody
    public Object relateProductList(int page, int rows,String searchType,String shopNameSearch,String productNameSearch,Integer productId){
        try {
            if (StringUtil.isEmptyOrNull(searchType)){
                return new ArrayList();
            }
            String findListSql = "SELECT p.id,p.`name` AS pname,s.`name` AS sname,pr.ord   " +
                    " FROM (qy_product p ,qy_seller s) LEFT JOIN qy_product_related pr " +
                    " ON (p.id = pr.related_product_id AND pr.product_id = "+productId+" ) ";

            String countSql = "SELECT COUNT(p.id)" +
                    " FROM (qy_product p ,qy_seller s) LEFT JOIN qy_product_related pr " +
                    " ON (p.id = pr.related_product_id AND pr.product_id = "+productId+" )   " ;

            StringBuilder connectSql = new StringBuilder("");
            if (searchType != null && !"".equals(searchType.trim()) && "0".equals(searchType.trim())){
                connectSql.append("  WHERE p.id IN  ");
            }else{
                connectSql.append("  WHERE p.id NOT IN ");
            }
            connectSql.append("  ( SELECT r.related_product_id  " +
                    "    FROM qy_product_related r " +
                    "     WHERE r.product_id = "+productId+" " +
                    "    )" +
                    "   AND p.seller_id = s.id");
            if (!StringUtil.isEmptyOrNull(shopNameSearch)){
                connectSql.append(" AND s.`name` LIKE '%"+shopNameSearch+"%'");
            }
            if (!StringUtil.isEmptyOrNull(productNameSearch)){
                connectSql.append(" AND p.`name` LIKE '%"+productNameSearch+"%'");
            }
            if("0".equals(searchType)) {
                connectSql.append(" ORDER BY pr.ord DESC,p.id");
            }else{
                connectSql.append(" ORDER BY p.id");
            }
            findListSql= findListSql + connectSql;
            countSql = countSql + connectSql;

            List<Map<String, Object>> list = baseService.findMapBySQL(findListSql, new HibernatePage(page, rows));
            int total = baseService.getRecordCountSQL(countSql);
            request.setAttribute("searchType",searchType);
            return  new EasyUIResponse(total, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加删除操作
     * @param dataType
     * @param productId
     * @param relatedProductId
     * @return
     */
    @RequestMapping("/operation.do")
    @ResponseBody
    public Object operation(Integer dataType,Integer productId,Integer relatedProductId){
        try {
            if (dataType != null && dataType == 0){
                String deleteSql = "DELETE FROM qy_product_related  " +
                        " WHERE product_id = "+productId+" " +
                        " AND related_product_id = "+relatedProductId+" ";
                baseService.excuteSQL(deleteSql);
            }else if (dataType != null && dataType == 1){
                ProductRelated productRelated = new ProductRelated();
                productRelated.setProductId(productId);
                productRelated.setRelatedProductId(relatedProductId);
                Date date = new Date();
                productRelated.setCreateTime(date);
                productRelated.setUpdateTime(date);
                baseService.save(productRelated);
            }
            return renderSuccess("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return renderError("操作失败");
        }
    }

    /**
     * 修改排序
     * @param productRelated
     * @return
     */
    @RequestMapping("/updateOrd.do")
    @ResponseBody
    public Object updateOrd(ProductRelated productRelated){
        try {
            String findSql=  "SELECT *  " +
                    " FROM qy_product_related r " +
                    " WHERE r.product_id = "+productRelated.getProductId()+"" +
                    " AND r.related_product_id = "+productRelated.getRelatedProductId()+"";
            ProductRelated pr = baseService.findSingleBeanBySQL(findSql, ProductRelated.class);
//            ProductRelated pr = baseService.get(ProductRelated.class, productRelated);
            BeanUtil.copyProperties(productRelated,pr);
            pr.setUpdateTime(new Date());
            baseService.update(pr);
            return renderSuccess("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("修改失败");
        }

    }

}
